package com.android.base_network.interceptor

import android.os.Handler
import android.os.Looper
import com.blankj.utilcode.util.ToastUtils
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody

/**
 * 响应拦截
 */
class ResponseInterceptor: Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {

        val request = chain.request()
        val response = chain.proceed(request)
        val responseBody = response.body?.string()
        println("Response body: $responseBody")

        // 检查 HTTP 状态码是否为 500,弹出土司
//        if (response.code == 500) {
//            // 在主线程中显示土司提示
//            Handler(Looper.getMainLooper()).post {
//                ToastUtils.showShort(response.message)
//            }
//        }

        // 如果需要对响应体做出修改，必须重新创建响应
        return response.newBuilder()
            .body(ResponseBody.create(response.body?.contentType(), responseBody ?: ""))
            .build()
    }
}